package com.mybook.app.service;

import com.github.pagehelper.PageInfo;
import com.mybook.app.core.BaseResponse;
import com.mybook.app.core.UserContext;
import com.mybook.app.core.WebPageInfo;
import com.mybook.app.exception.DataIsEmptyException;
import com.mybook.app.mapper.BookInfoMapper;
import com.mybook.app.mapper.CollectInfoMapper;
import com.mybook.app.model.CollectInfo;
import com.mybook.app.renum.CollectFlagEnum;
import com.mybook.app.request.CollectPageRequest;
import com.mybook.app.request.CollectRequest;
import com.mybook.app.response.CollectionDetail;
import com.mybook.app.utils.LatLngUtils;
import com.mybook.app.utils.Logger;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by zjutsw on 2017/11/7.
 */
@Service
public class CollectService {

    @Autowired
    private CollectInfoMapper collectInfoMapper;
    @Autowired
    private BookInfoMapper bookInfoMapper;
    @Autowired
    private UserService userService;

    /**
     * 添加收藏或取消收藏
     */
    public void add(CollectRequest collectRequest) {
        // 假如添加收藏，查询是否收藏
        if (collectRequest.getFlag().equals(CollectFlagEnum.add)) {
            CollectInfo collectInfo = collectInfoMapper.findCollectInfoByIdAndBId(UserContext.getCurrentuserId().get(), collectRequest.getbId());
            if (null == collectInfo) {
                // 添加收藏
                Logger.info(this, "未添加收藏，添加收藏");
                collectInfo = new CollectInfo();
                collectInfo.setId(UserContext.getCurrentuserId().get());
                collectInfo.setbId(collectRequest.getbId());
                collectInfo.setTime(new Date());
                int row = collectInfoMapper.insertSelective(collectInfo);
                Logger.info(this, "收藏是否成功：" + row);
            }
        } else if (collectRequest.getFlag().equals(CollectFlagEnum.cancel)) {
            CollectInfo collectInfo = collectInfoMapper.findCollectInfoByIdAndBId(UserContext.getCurrentuserId().get(), collectRequest.getbId());
            if (null != collectInfo) {
                // 添加收藏
                Logger.info(this, "删除收藏");
                int row = collectInfoMapper.delete(collectInfo);
                Logger.info(this, "删除收藏是否成功：" + row);
            }
        }
    }

    /**
     * 我的收藏
     */
    public BaseResponse list(CollectPageRequest collectPageRequest) {
        collectPageRequest.setId(UserContext.getCurrentuserId().get());
        List<CollectionDetail> collectInfoList = collectInfoMapper.findCollectInfoAndBookInfoAndUserInfoById(collectPageRequest);
        if (CollectionUtils.isEmpty(collectInfoList)) {
            //收藏列表
            Logger.info(this, "查询收藏列表为空");
            throw new DataIsEmptyException();
        }
        PageInfo<CollectionDetail> pageInfo = new PageInfo<>(collectInfoList);
        // 分页信息
        WebPageInfo webPageInfo = new WebPageInfo();
        BeanUtils.copyProperties(pageInfo, webPageInfo);

        // 数据转换
        List<CollectionDetail> collectionDetails = collectInfoList.stream().map(dto -> {
            /*CollectionDetail collectionDetail = new CollectionDetail();
            collectionDetail.setColllectDate(dto.getTime());
            // 数据信息
            if (null != dto.getBookInfo()) {
                BookInfo bookInfo = dto.getBookInfo();
                collectionDetail.setAuthor(bookInfo.getAuthor());
                collectionDetail.setbId(bookInfo.getId());
                collectionDetail.setBorrowTimes(bookInfo.getBorrows());
                collectionDetail.setName(bookInfo.getName());
                collectionDetail.setPress(bookInfo.getPress());
                collectionDetail.setStar(bookInfo.getStar());
                collectionDetail.setUrl1(bookInfo.getUrl1());
            }*/
            //距离
            if (null != dto.getCoor()) {
                dto.setDistance(LatLngUtils.getDistance(userService.getCoor(), dto.getCoor()));
            }
            return dto;
        }).collect(Collectors.toList());

        BaseResponse baseResponse = new BaseResponse(collectionDetails, webPageInfo);
        return baseResponse;

    }


}


